home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WFILE.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-15  |  10.8 KB  |  303 lines

  1. #ifndef WFileIncluded
  2. #define WFileIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. //       phone:  (406)543-1928
  8. //  CompuServe:  72707,207
  9. //    Internet:  72707.207@CompuServe.com
  10.  
  11. /*
  12.  
  13. Although these routines lean on ANSI stdio functions, many of them work
  14. somewhat differently.   "File" is for binary mode and "TextFile" is for
  15. text mode. The constructor will open the file (create it if it doesn't
  16. exist).  It is the applications programmers responsibility to make sure
  17. that the filename given is valid and possible.  If the file cannot be
  18. opened, the program will be stopped.
  19.  
  20. Binary files:
  21.  
  22. Reading and writing may be done to any byte location that will fit into a
  23. long.  Writing more than a byte beyond the EOF will simply generate garbage
  24. characters from the EOF up to the point you are now writing.  Reading
  25. beyond EOF will result in a garbage read and the Read function will return
  26. False.
  27.  
  28. Text files:
  29.  
  30. After opening, the first read will be done from the beginning of the file
  31. and the first write will be done to the end of the file.
  32.  
  33. Note!: if a special buffer size has been requested and *denied* (due to
  34. lack of heap space), a beep will sound but file access will continue
  35. with the default buffer size.
  36.  
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <io.h>
  41. #include <WMisc.h>
  42. #include <WVec.h>
  43.  
  44. #ifdef MAJORBBS
  45.  
  46.   Bool FileExists(const char*);
  47.  
  48. #else
  49.  
  50.   #define FileExists(FileName) (!access(FileName,0))
  51.     // returns True if the file exists
  52.  
  53. #endif
  54.  
  55.  
  56. char CurDiskDrive();
  57.  
  58. long DiskSpace(const char Drive='.');  // '.' means default drive
  59.   // the value returned is in "K" or "kilobytes" or number of 1024 byte blocks
  60.   // this is in case this library is on a computer that can handle capacity
  61.   // of more than 2 gigs that a "long" could represent.
  62.  
  63. long FileSize(const char* FileName);
  64.   // the value returned is in bytes
  65.  
  66. #define WriteThing(A) Write(&A,sizeof(A))
  67. #define ReadThing(A) Read(&A,sizeof(A))
  68.   // use only on things where the size can be properly determined with
  69.   // "sizeof".  Use only with "File" not "TextFile"
  70.  
  71. void DeleteFile(const char* FileName);
  72.   // if the file is deletable, it's deleted
  73.  
  74. const ReadAndWrite=0;
  75. const ReadOnly=1;
  76.  
  77. class LowLevelFile
  78.   {
  79.       FILE* FilePointer;
  80.       Bool Open;
  81.       char* GivenFileName;
  82.       char* Buf; // for future use to change the size of the buffer
  83.       void InternalInit(int);
  84.       friend class File;
  85.       friend class TextFile;
  86.       friend class RecFileRef;
  87.       friend class RecFile;
  88.     public:
  89.       LowLevelFile(const char* FileName,const char* Mode, int BufSize);
  90.       //LowLevelFile(const char* FileName,int Share,const char* Mode, int BufSize);
  91.       ~LowLevelFile() {Close();}
  92.       void Close();  // you can close the file early if you want
  93.       void Flush() {fflush(FilePointer);}
  94.         // flush your write buffers out to disk
  95.       long CurPos() {return ftell(FilePointer);}
  96.         // current file position:  where you are about to write to or read from
  97.       void Seek(long Offset) {fseek(FilePointer,Offset,0);}
  98.       void SeekBOF(){fseek(FilePointer,0,0);}
  99.       void SeekEOF(){fseek(FilePointer,0,2);}
  100.       long Size();  //  the file size in bytes
  101.       Bool EndOfFile() {return feof(FilePointer);}
  102.       #ifdef MAJORBBS
  103.         void* operator new(size_t size){return malloc(size);}
  104.         void  operator delete(void* p) {free(p);}
  105.       #endif
  106.   };
  107.  
  108. class File:public LowLevelFile
  109.   {
  110.     public:
  111.       File(const char* FileName,Bool Mode=ReadAndWrite,int BufSize=BUFSIZ);
  112.       Bool Read(void *Buffer,int Size=1);
  113.       Bool Read(ByteVector& BV,int Size=1);
  114.       void Write(const void *Buffer,int Size=1);
  115.   };
  116.  
  117. class RecFile:public LowLevelFile
  118.   {
  119.       int RecSize;
  120.     public:
  121.       RecFile(const char* FileName,int RecordSize,Bool Mode,int BufSize);
  122.       Bool Read(void *Buffer);
  123.       Bool Read(void *Buffer,int Quan);
  124.       void Seek(long RecNum);
  125.       long CurRec();  // record number
  126.       long Size();  // number of recs
  127.       void Write(const void *Buffer);
  128.       void Write(const void *Buffer,int Quan);
  129.   };
  130.  
  131. #define CreateRecFileClass(ClassName,StructType)                        \
  132.                                                                         \
  133. class ClassName;                                                        \
  134.                                                                         \
  135. class ClassName ## Ref                                                  \
  136.   {                                                                     \
  137.       ClassName* F;                                                     \
  138.       ClassName ## Ref(ClassName* XF){F=XF;}                            \
  139.       friend class ClassName;                                           \
  140.     public:                                                             \
  141.       void operator=(const StructType& X);                              \
  142.       operator StructType();                                            \
  143.       void* operator new(size_t size){return malloc(size);}             \
  144.       void  operator delete(void* p) {free(p);}                         \
  145.   };                                                                    \
  146.                                                                         \
  147. class ClassName:public RecFile                                          \
  148.   {                                                                     \
  149.     public:                                                             \
  150.       ClassName(const char* FileName,Bool Mode=ReadAndWrite,            \
  151.           int BufSize=BUFSIZ):                                          \
  152.           RecFile(FileName,sizeof(StructType),Mode,BufSize){}           \
  153.       Bool Read(StructType& X){return RecFile::Read(&X);}               \
  154.       Bool Read(StructType* X,int Q){return RecFile::Read(X,Q);}        \
  155.       void Write(const StructType& X){RecFile::Write(&X);}              \
  156.       void Write(const StructType* X,int Q){RecFile::Write(X,Q);}       \
  157.       ClassName ## Ref operator[](long RecNum)                          \
  158.         {Seek(RecNum); return ClassName ## Ref(this);}                  \
  159.   };                                                                    \
  160.                                                                         \
  161. inline void ClassName ## Ref::operator=(const StructType& X)            \
  162.   {F->Write(X);}                                                        \
  163. inline void ClassName ## Ref::operator StructType()                     \
  164.   {StructType X; F->Read(X); return X;}
  165.  
  166.  
  167. // friend void operator=(StructType& X,ClassName ## Ref R);          \
  168. //inline void operator=(StructType& X,ClassName ## Ref R)                 \
  169. //  {(R.F)->Read(X);}
  170.  
  171. #ifdef __BORLANDC__
  172.   #define FTMRO "rt"
  173.   #define FTMRW "r+t"
  174. #else
  175.   #define FTMRO "r"
  176.   #define FTMRW "r+"
  177. #endif
  178.  
  179. class TextFile:public LowLevelFile
  180.   {
  181.       Bool Started;  // used to figure out whether we're starting off appending or reading
  182.     public:
  183.       TextFile(const char* FileName,Bool Mode=ReadAndWrite,int BufSize=BUFSIZ):
  184.           LowLevelFile(FileName,((Mode==ReadOnly)?(FTMRO):(FTMRW)),BufSize)
  185.           {Started=False;}
  186.       void Seek(long Offset) {Started=True; LowLevelFile::Seek(Offset);}
  187.       Bool Read(char* S);
  188.       void Write(const char* S);
  189.       void WriteLine(const char* S="");
  190.   };
  191.  
  192. void FileCopying(File& DestFile, File& SourceFile, long Size);
  193.   // will not modify file positions before writing
  194.  
  195. void CopyFile(const char* DestFile, const char* SourceFile);
  196.   // source file must be created.  if dest file already exists,
  197.   // it will be deleted
  198.  
  199. Word CRC(File& F,long FSize);  // CRC of file starting at current pos
  200. Word CRC(File& F);  // CRC of entire file
  201.  
  202.  
  203. /*
  204.  
  205. Example of a file of records:
  206.  
  207. struct XType
  208.   {
  209.     long a,b,c;
  210.   };
  211.  
  212. CreateRecFileClass(XFile,XType);
  213.  
  214. main()
  215.   {
  216.     XType X;
  217.     XFile F("X.BIN");
  218.     F[0]=X;
  219.     X=F[0];
  220.   }
  221.  
  222. */
  223.  
  224. /*
  225.   Features to add:
  226.      1)  Make it so that a person cannot write beyond the end of their block
  227.  
  228. */
  229.  
  230. class TokenFile: public File
  231.   {
  232.       long MaxTokens;
  233.       long FirstFreeBlock;
  234.       long NextFreeToken;
  235.       void GetFreeInfo(long Pos, long& Size, long& NextBlock);
  236.         // used to traverse free list
  237.       long FindSpace(long FSize);
  238.         // returns a block number that can hold "FSize" and updates the
  239.         // free list if needed
  240.       void Delete(long Pos, long Size);
  241.         // adds this info to the free list if its big enough
  242.       void SeekIndexSlot(long Token) {File::Seek(Token*8);}
  243.       void ReadCurIndexSlot(long& Pos, long& Size);
  244.       friend Bool TokenExists(const char*,long);
  245.       friend void ShowFreeList(const char*);
  246.     public:
  247.       TokenFile(const char* FileName, int BufSize=BUFSIZ);
  248.       long Seek(long Token);
  249.         /*  returns the size of your object (0 if it doesn't exist).  If
  250.         object is found, file pointer is moved to your objects storage
  251.         location.  You may then read your data with any binary file type
  252.         read.  There will be nothing to stop you from reading too much */
  253.       void WritePrep(long Token,long DataSize);
  254.         /* sets up Token with a data area of just the right size.  Any
  255.         previous data under that Token is deleted.  File pointer is left
  256.         where writing may begin */
  257.       void Delete(long Token);
  258.         // makes the space that was taken by Token's object available
  259.       long MaxToken(){return MaxTokens;}
  260.       Bool TokenExists(long Token);
  261.       void Extract(long Token,char* FileName);
  262.       long NewToken();
  263.         // will provide an unused token number
  264.       long NewToken(long DataSize);
  265.         // will allocate the space and provide an unused token number
  266.   };
  267.  
  268. Bool TokenExists(const char* FileName, long Token);
  269.  
  270. void SaveBootRec(TokenFile& F,const char* CName);
  271.   // Optional...  Saves the video device boot info
  272.  
  273. /* This class inherits all of the properties of the (binary) File class.
  274. It adds the feature of being able to store many little files into one
  275. larger file.  Given a token ("access code", "key") value you may store and
  276. retrieve your data pretty much like any other binary file.
  277.  
  278. Example of writing a struct of type XType:
  279.  
  280.    XType X;
  281.    TokenFile F("DATA.BIN");
  282.    F.WritePrep(326,sizeof(X)); // you pick your own Token number and object size
  283.    F.WriteThing(X);
  284.  
  285. Example of reading the same struct:
  286.  
  287.    XType X;
  288.    TokenFile F("DATA.BIN");
  289.    if (F.Seek(326)) F.ReadThing(X);
  290.    else FatalError("cannot find my X thing in file DATA.BIN");
  291.  
  292. */
  293.  
  294. // all this stuff is sent out to the good ole stdprn
  295. void Print(const char* Text="");
  296. void Print(char C);
  297. void PrintLine(const char* X);
  298. void PrintFile(const char* FileName,int HeaderLines=0,Bool PageNumbers=False,
  299.     VoidFuncPtr GrindFunc=NULL);
  300. inline void PrintFormFeed(){PrintLine("\f");}
  301.  
  302. #endif
  303.